use sea_orm::{
    ColumnTrait, DbBackend, EntityTrait, JoinType, QueryFilter, QueryOrder, QuerySelect,
    QueryTrait, RelationTrait,
};
use sky_common::constant;
use sky_pojo::{
    entities::{dish, order_detail, orders, setmeal_dish},
    vo::dish::DishItemVO,
};
use tokio;

fn test_1() -> String {
    let mut stmt = order_detail::Entity::find()
        .select_only()
        .join_rev(
            JoinType::InnerJoin,
            orders::Entity::belongs_to(order_detail::Entity)
                .from(orders::Column::Id)
                .to(order_detail::Column::OrderId)
                .into(),
        )
        .column_as(order_detail::Column::Name, "name")
        .column_as(order_detail::Column::Number.sum(), "number")
        .filter(orders::Column::Status.eq(constant::order::COMPLETED));

    let begin = chrono::Utc::now().naive_utc();
    let end = chrono::Utc::now().naive_utc();

    stmt = stmt.filter(orders::Column::OrderTime.gte(begin));

    stmt = stmt.filter(orders::Column::OrderTime.lte(end));

    let str = stmt
        .group_by(order_detail::Column::Name)
        .order_by_desc(order_detail::Column::Number.sum())
        .limit(10)
        .build(DbBackend::MySql)
        .to_string();

    str
}

fn test_2(setmeal_id: i64) -> String {
    setmeal_dish::Entity::find()
        .select_only()
        .column(setmeal_dish::Column::Name)
        .column(setmeal_dish::Column::Copies)
        .column(dish::Column::Image)
        .column(dish::Column::Description)
        .join(JoinType::LeftJoin, setmeal_dish::Relation::Dish.def())
        .filter(setmeal_dish::Column::SetmealId.eq(setmeal_id))
        .build(DbBackend::MySql)
        .to_string()
}

#[tokio::main]
async fn main() {
    let str = test_2(1);
    println!("{}", str)
}
